//
//  StoryViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "StoryViewController.h"
#import "TAMacro.h"
#import "ClassifyViewController.h"
#import "BrandViewController.h"
#import "HomeViewController.h"
#import "GiftViewController.h"
#import "TopicViewController.h"
#import "UIView+JDExtension.h"
#import "TextView.h"

#import "CellDetailViewController.h"

@interface StoryViewController () <UITextFieldDelegate>
@property (nonatomic, strong) UIView *aView;

/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIView *titlesView;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *left;
@property (nonatomic, strong) UIButton *right;

@property (nonatomic, strong) UITextField *searchTextField;
@property (nonatomic, strong) UIButton *grayBackButton;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@end

@implementation StoryViewController
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.094 alpha:1.000];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTopView];
    [self createBarbutton];
    [self createSearch];

    [self setupChildViewController];
    // 设置顶部的标签栏
    [self setupTitlesView];
    
}
- (void)createBarbutton {
    self.left = [UIButton buttonWithType:UIButtonTypeCustom];
    self.left.frame = CGRectMake(20, 30, 20, 20);
   [self.left addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.left setImage:[UIImage imageNamed:@"19"] forState:UIControlStateNormal];
    [self.topView addSubview:self.left];
    // 购物车
//    self.right = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.right.frame = CGRectMake(WIDTH - 40, 30, 20, 20);
//    [self.right setImage:[UIImage imageNamed:@"20"] forState:UIControlStateNormal];
//    [self.topView addSubview:self.right];
}

- (void)searchAction:(UIButton *)button {
    
    if (self.searchTextField.bounds.size.height == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.titleLabel.frame = CGRectMake(50, 28, 60, 20);
            self.searchTextField.frame = CGRectMake(100, 28, 200, 20);
        }];
    } else {
        [self.searchTextField resignFirstResponder];
        [UIView animateWithDuration:0.5 animations:^{
            self.titleLabel.frame = CGRectMake(WIDTH / 2 - 25 , 28, 50, 20);
            self.searchTextField.frame = CGRectMake(10, 0, WIDTH - 20, 0);
            [self.searchTextField setText:@""];
        }];
    }
}

- (void)createSearch {
    self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, WIDTH - 20, 0)];
    self.searchTextField.delegate = self;
    self.searchTextField.placeholder = @"                       search";

    self.searchTextField.clearsOnBeginEditing = NO;
    // 设置textField的右侧的清除键
    self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTextField.backgroundColor = [UIColor whiteColor];
    self.searchTextField.keyboardType = UIKeyboardTypeDefault;
    self.searchTextField.returnKeyType = UIReturnKeySearch;
    self.searchTextField.font = [UIFont systemFontOfSize:13];
    [self.searchTextField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
    self.searchTextField.backgroundColor = [UIColor colorWithWhite:0.422 alpha:1.000];
    [self.searchTextField setValue:[UIColor colorWithWhite:0.619 alpha:1.000] forKeyPath:@"_placeholderLabel.textColor"];
    // 改变光标的颜色
    [[UITextField appearance] setTintColor:[UIColor colorWithWhite:0.227 alpha:1.000]];
    // 可以设置textFieldplaceholder各种居中
    self.searchTextField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
//    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
//    style.lineBreakMode = NSLineBreakByTruncatingTail;
//    style.alignment = self.searchTextField.textAlignment;
//    NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.searchTextField.font, NSFontAttributeName, self.searchTextField.placeholder, NSForegroundColorAttributeName, nil];
//    CGRect placeholderRect = CGRectMake(self.searchTextField.bounds.origin.x+10, (self.searchTextField.bounds.size.height- self.searchTextField.font.pointSize)/2, self.searchTextField.bounds.size.width, self.searchTextField.font.pointSize);//设置距离
//    self.searchTextField.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;

//    [self.searchTextField.placeholder drawInRect:placeholderRect withAttributes:attr];

    
    [self.topView addSubview:self.searchTextField];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    CellDetailViewController *searchVC = [[CellDetailViewController alloc] init];
    if (self.searchTextField.text.length != 0) {
        searchVC.textStr = self.searchTextField.text;
        [self.navigationController pushViewController:searchVC animated:YES];
        [self.searchTextField resignFirstResponder];
        [UIView animateWithDuration:0.5 animations:^{
            self.searchTextField.frame = CGRectMake(100, 0, WIDTH - 20, 0);
            self.titleLabel.frame = CGRectMake(WIDTH / 2 - 25, 28, 50, 20);
        }];
        [self.searchTextField setText:@""];

    } else {
        [UIView animateWithDuration:0.5 animations:^{
            [self.searchTextField resignFirstResponder];
            self.searchTextField.frame = CGRectMake(10, 0, WIDTH - 20, 0);
            self.titleLabel.frame = CGRectMake(WIDTH / 2 - 25, 28, 50, 20);
        }];
    }
    return YES;
}

//- (void)grayButtonAction:(UIButton *)button {
//    [UIView animateWithDuration:0.5 animations:^{
//        self.grayBackButton.frame = CGRectMake(0, 0, WIDTH, 0);
//        self.searchTextField.frame = CGRectMake(10, 0, WIDTH - 20, 0);
//    }];
//    // 回收键盘
//    [self.searchTextField resignFirstResponder];
//}


- (void)createTopView {
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.topView.backgroundColor = [UIColor colorWithWhite:0.176 alpha:1.000];
    [self.view addSubview:self.topView];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 25 , 28, 50, 20)];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.titleLabel.text = @"商店";
    self.titleLabel.textAlignment = 1;
    self.titleLabel.textColor = [UIColor whiteColor];
    [self.topView addSubview:self.titleLabel];
    
}

- (void)setupChildViewController
{
    NSArray *vcArray = [NSArray array];
    ClassifyViewController *classVC = [[ClassifyViewController alloc] init];
    BrandViewController *brandVC = [[BrandViewController alloc] init];
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    TopicViewController *topicVC = [[TopicViewController alloc] init];
    GiftViewController *giftVC = [[GiftViewController alloc] init];
    vcArray = @[classVC, brandVC, homeVC, topicVC, giftVC];

    for (int i = 0; i < 5; i++) {
        [self.viewControllers addObject:vcArray[i]];
    }
}

/**
 * 设置顶部的标签栏
 */
- (void)setupTitlesView
{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor colorWithWhite:0.176 alpha:1.000];
    titlesView.width = self.view.width;
    titlesView.height = 35;
    titlesView.y = 64;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 底部的红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor whiteColor];
    indicatorView.height = 2;
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    CGFloat width = titlesView.width / self.viewControllers.count;
    CGFloat height = titlesView.height;
    NSArray *titleArray = [NSArray array];
    titleArray = @[@"分类", @"品牌", @"首页", @"专题", @"礼物"];
    for (NSInteger i = 0; i<self.viewControllers.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        NSString *title = [NSString stringWithFormat:@"%@", titleArray[i]];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
        }
    }
    
    [titlesView addSubview:indicatorView];
}

- (void)titleClick:(UIButton *)button
{
    [self buttonStateChange:button];
    // 滚动
    [self setShowingIndex:button.tag animate:YES];
}

- (void)buttonStateChange:(UIButton *)button
{
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    
}

#pragma mark -SJSwitchViewControllerDelegate

- (NSInteger)numberOfSwitchViewController
{
    return self.viewControllers.count;
}

- (UIViewController *)switchViewControllerDidGetViewControllerAtIndex:(NSUInteger)index
{
    return [self.viewControllers objectAtIndex:index];
}

-(void)switchViewControllerDidStopAtIndex:(NSInteger)index
{
    UIButton *button = self.titlesView.subviews[index];
    [self buttonStateChange:button];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
